package com.threeti.mecool.mobile.interfaces.assembler;

import com.threeti.mecool.core.application.project.ProjectService;
import com.threeti.mecool.core.domain.model.acl.User;
import com.threeti.mecool.core.domain.model.project.Project;
import com.threeti.mecool.core.domain.model.project.ProjectStore;
import com.threeti.mecool.framework.util.Lists;
import com.threeti.mecool.mobile.interfaces.dto.ProjectDto;
import com.threeti.mecool.mobile.interfaces.dto.ProjectStoreDto;
import com.threeti.mecool.mobile.interfaces.dto.ProjectWithOnlyStoresDto;
import com.threeti.mecool.mobile.interfaces.dto.ProjectWithStoresAndSellIntoDto;
import com.threeti.mecool.mobile.util.DozerMapperSingleton;
import org.dozer.Mapper;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

public abstract class ProjectAssembler {
    public static List<ProjectDto> toDto(List<Project> src) {
        Mapper mapper = DozerMapperSingleton.getInstance();
        List<ProjectDto> result = new LinkedList<ProjectDto>();
        for (Project project : src) {
            result.add(mapper.map(project, ProjectDto.class));
        }
        return result;
    }

    public static ProjectWithOnlyStoresDto toDto(Project srcDom) {
        Mapper transfer = DozerMapperSingleton.getInstance();
        ProjectWithOnlyStoresDto destDto = new ProjectWithOnlyStoresDto();
        transfer.map(srcDom, destDto);
        return destDto;
    }

    public static ProjectWithStoresAndSellIntoDto toDto1(Project srcDom) {
        Mapper transfer = DozerMapperSingleton.getInstance();
        ProjectWithStoresAndSellIntoDto destDto = new ProjectWithStoresAndSellIntoDto();
        transfer.map(srcDom, destDto);
        return destDto;
    }

    public static ProjectStoreDto toDto(ProjectStore srcDom) {
        Mapper transfer = DozerMapperSingleton.getInstance();
        ProjectStoreDto destDto = new ProjectStoreDto();
        transfer.map(srcDom, destDto);
        return destDto;
    }

    public static List<ProjectWithOnlyStoresDto> toDtoList(List<Project> srcDoms, User user, ProjectService projService) {
        List<ProjectWithOnlyStoresDto> dtos = Lists.newArrayList();

        for (Project project : srcDoms) {
            if (project != null && project.getId() != null) {
                ProjectWithOnlyStoresDto projDto = toDto(project);

                List<ProjectStore> stores = projService.findStoresPreAssignedToUserWithinTheProject(user, project.getId());
                if (!CollectionUtils.isEmpty(stores)) {
                    for (ProjectStore projectStore : stores) {
                        projDto.addProjectStoreDto(ProjectAssembler.toDto(projectStore));
                    }
                }
                dtos.add(projDto);
            }
        }
        return dtos;
    }
}
